/*
	X - Horizontal
	Y - Vertical
	
	maps: id, width, height, name, school_id
	mapPieces: pos_x, pos_y, width, height, filename
	mapLocs: id, pos_x, pos_y, name, address, isStore
*/

function inside(x, y, x1, y1, x2, y2){
	return (x <= x2 && x >= x1) && (y <= y2 && y >= y1);
}

function textWidth(obj, text){
	var calc = '<span style="display:none;' + obj.attr("style") + '">' + text + '</span>';
	$('body').append(calc);
	var width = $('body').find('span:last').width();
	$('body').find('span:last').remove();
	return width;
}

var MapCtrl = dojo.declare(null, {
	//constructor/desctuctor
	constructor : mapCtrl_contructor,
	destroy : mapCtrl_destructor,
	
	//methods
	init : mapCtrl_init,
	loadMap : mapCtrl_loadMap,
	showMap : mapCtrl_showMap,
	showMapLocPic : mapCtrl_showMapLocPic,
	
	//slots
	sizeChanged : mapCtrl_sizeChanged,
	updateLoginState : mapCtrl_updateLoginState
});

function mapCtrl_contructor(){
	this._html = "";
	this._curMap = -1;
	this._mapLocX = 0;
	this._mapLocY = 0;
	this._maps = new Array();
	this._mapPieces = new Array();
	this._mapLocs = new Array();
	this._mapLocPics = new Array();
	this._mapLoader = null;
	this._parameter = {};
	
	this._connections = new Array();
}

function mapCtrl_destructor(){
	for (var i = 0; i < this._connections.length; i ++) dojo.disconnect(this._connections[i]);
	
	if (this._mapLoader != null) this._mapLoader.destroy();
}

function mapCtrl_init(parameter){
	var obj = this;
	this._connections.push(dojo.connect(globalCtrl, "userLoginStateChanged", this, "updateLoginState"));
	if (parameter != null) this._parameter = parameter;
	if (this._parameter.floater != null)
		this._connections.push(dojo.connect(this._parameter.floater, "sizeChanged", this, "sizeChanged"));
	
	this._html = $("#map_page_bound");
	
	$.getJSON("/map/getSchoolMaps", function(data){
		obj._maps = data;
		for (var i = 0; i < obj._maps.length; i ++)
			obj._html.find("#toolbar #input_school").append("<option value='" + obj._maps[i][0] + "'>" + obj._maps[i][3] + "</option>");
		obj.loadMap(0, obj._parameter.needSlide);
	});
}

function mapCtrl_loadMap(id, needSlide){
	var obj = this;
	var readyCount = 0;
	if (needSlide == null) needSlide = true;
	function checkReady(){
		readyCount ++;
		if (readyCount == 2){
			obj._html.find("#page_main").fadeIn();
			var frameWidth = obj._html.find("#map").width();
			var frameHeight = obj._html.find("#map").height();
			obj._html.find("#map #map_elementlist").width(obj._maps[obj._curMap][1]);
			obj._html.find("#map #map_elementlist").height(obj._maps[obj._curMap][2]);
			var mapPos = $("#map").offset();
			obj._html.find("#map #map_all").draggable({"containment" : 
				[- obj._maps[obj._curMap][1] + frameWidth + mapPos.left, - obj._maps[obj._curMap][2] + frameHeight + mapPos.top, mapPos.left, mapPos.top],
				"cursor" : "move", "stop" : function(){
					obj.showMap();
				}
			});
		}
	}
	
	if (obj.curPage == "map" && obj._curMap == id) return;
	obj._html.find("#page_main").hide();
	if (obj.curPage != "map"){
		if (needSlide){
			$("#page_" + obj.curPage).effect("slide", {mode : "hide", direction : "right"}, "slow");
			obj._html.effect("slide", {mode : "show"}, "slow", showIt);
		}  else {
			$("#page_" + obj.curPage).hide();
			obj._html.show();
			showIt();
		}  
		obj.curPage = "map";
	}  else {
		showIt();
	}
	function showIt(){
		obj._html.show();
		if (obj._curMap != id){
			obj._curMap = id;
			obj._html.find("#map #map_elementlist").html("");
			$.getJSON("/map/getSchoolMapPiecesByMapId", {"id" : obj._maps[id][0]}, function(data){
				obj.mapPieces = data;
				for (var i = 0; i < obj.mapPieces.length; i ++){
					obj._html.find("#map #map_elementlist").append("<img class='map_piece' id='map_piece_" + i + "'></img>");
					mapPiece = obj._html.find("#map #map_elementlist #map_piece_" + i);
					mapPiece.css("left", obj.mapPieces[i][0]);
					mapPiece.css("top", obj.mapPieces[i][1]);
					mapPiece.css("width", obj.mapPieces[i][2]);
					mapPiece.css("height", obj.mapPieces[i][3]);
				}
				checkReady();
				obj.showMap(obj._maps[id][1] / 2, obj._maps[id][2] / 2);
			});
			$.getJSON("/map/getSchoolMapLocsByMapId", {"id" : obj._maps[id][0]}, function(data){
				obj.mapLocs = data;
				for (var i = 0; i < obj.mapLocs.length; i ++){
					obj._html.find("#map #map_elementlist").append("<div class='map_loc' href='javascript:showMapLocDetails(" + i +")' id='map_loc_" + i + "'></div>");
					mapLoc = obj._html.find("#map #map_elementlist #map_loc_" + i);
					mapLoc.css("left", obj.mapLocs[i][1]);
					mapLoc.css("top", obj.mapLocs[i][2]);
					mapLoc.attr("arrayId", i);
					mapLoc.click(function(){
						var id = $(this).attr("arrayId");
						var leftWidth = textWidth($("#loc_info #store_name"), obj.mapLocs[id][3]) + 20;
						if (leftWidth < 150) leftWidth = 150;
						$("#loc_info #column_left").width(leftWidth);
						$("#loc_info #store_name").html(obj.mapLocs[id][3]);
						$("#loc_info #store_address").html(obj.mapLocs[id][4]);
						$("#loc_info #panel_detail_link").attr("arrayId", id);
						$("#loc_info #panel_detail_link").click(function(){
							var id = $("#loc_info #panel_detail_link").attr("arrayId");
							obj.loadMapLocPage(id);
						});
						$("#loc_info").css("left", obj.mapLocs[id][1] + 10);
						$("#loc_info").css("top", obj.mapLocs[id][2] + 10);
						$("#loc_info #store_pic").html("");
						$.getJSON("/map/getSchoolMapLocPics", {"id" : obj.mapLocs[id][0]}, function(data){
							for (var j = 0; j < data.length; j ++){
								if (data[j][0] == 'small'){
									$("#loc_info #store_pic").html("<img src='" + data[j][1] + "'></img>");
									break;
								}
							}
							$("#loc_info #column_left_margin").height($("#loc_info").height() - $("#loc_info #store_name").height() - 
								$("#loc_info #store_address").height() - $("#loc_info #store_panel").height());
						});
						$("#loc_info").show();
					});
				}
				checkReady();
				obj.showMap(obj._maps[id][1] / 2, obj._maps[id][2] / 2);
			});
		}  else {
			checkReady();
			checkReady();
		}  
	}
}

function mapCtrl_showMap(x, y){
	var obj = this;
	if (obj.curPage != "map") return;
	var frameWidth = obj._html.find("#map").width();
	var frameHeight = obj._html.find("#map").height();
	if (x == null || y == null){
		obj.mapLocX = frameWidth / 2 - obj._html.find("#map #map_all").position().left;
		obj.mapLocY = frameHeight / 2 - obj._html.find("#map #map_all").position().top;
	}  else {
		obj.mapLocX = x;
		obj.mapLocY = y;
	}
	var minX = obj.mapLocX - frameWidth / 2, minY = obj.mapLocY - frameHeight / 2;
	var maxX = obj.mapLocX + frameWidth / 2, maxY = obj.mapLocY + frameHeight / 2;
	obj._html.find("#map #map_all").css("left", (- minX));
	obj._html.find("#map #map_all").css("top", (- minY));
	for (var i = 0; i < obj.mapPieces.length; i ++){
		var x1 = obj.mapPieces[i][0], x2 = obj.mapPieces[i][0] + obj.mapPieces[i][2];
		var y1 = obj.mapPieces[i][1], y2 = obj.mapPieces[i][1] + obj.mapPieces[i][3];
		if (inside(x1, y1, minX, maxX, minY, maxY) || inside(x2, y2, minX, maxX, minY, maxY) || 
			inside(minX, minY, x1, y1, x2, y2) || (minY <= y2 && minY >= y1 && x1 <= maxX && x1 >= minX) ||
			(minX <= x2 && minX >= x1 && y1 <= maxY && y1 >= minY)){
				obj._html.find("#map #map_piece_" + i).attr("src", obj.mapPieces[i][4]);
		}
	}
}

function mapCtrl_showMapLocPic(id){
	var obj = this;
	if (obj.curPage != "loc" || obj._curMapLocPic == id) return;
	obj.locPage.find("#locinfo #gallery_img_" + obj._curMapLocPic).hide();
	obj.locPage.find("#locinfo #gallery_img_" + id).show();
	obj._curMapLocPic = id;
}

function mapCtrl_sizeChanged(){
	var obj = this;
	if (obj.curPage == "map"){
		obj._html.find("#page_main").fadeIn();
		var frameWidth = obj._html.find("#map").width();
		var frameHeight = obj._html.find("#map").height();
		obj._html.find("#map #map_elementlist").width(obj._maps[obj._curMap][1]);
		obj._html.find("#map #map_elementlist").height(obj._maps[obj._curMap][2]);
		var mapPos = $("#map").offset();
		obj._html.find("#map #map_all").draggable({"containment" : 
			[- obj._maps[obj._curMap][1] + frameWidth + mapPos.left, - obj._maps[obj._curMap][2] + frameHeight + mapPos.top, mapPos.left, mapPos.top],
			"cursor" : "move"	
		});
		
		obj.showMap(obj.mapLocX, obj.mapLocY);
	}
}

function mapCtrl_updateLoginState(){
	this.loadMapLocLike();
}

if (dojo.getObject("mapCtrl") == null) mapCtrl = new MapCtrl();